﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using Isis.Helpers.Interfaces;
using Isis.Model;

namespace Isis.Helpers.Impl
{
    public class TableRenderingHelper : ITableRenderingHelper
    {
        public string RenderTableHeader(string tableHeading)
        {
            return string.Format("<table class=\"info-table\"><caption>{0}</caption>", tableHeading);
        }

        public string RenderHeaderRow(params string[] columnHeadings)
        {
            return RenderHeaderRow(columnHeadings as IEnumerable<string>);
        }

        public string RenderHeaderRow(IEnumerable<string> columnHeadings)
        {
            var htmlHeadings = new StringBuilder("<thead><tr>");

            foreach(string heading in columnHeadings)
            {
                htmlHeadings.Append(string.Format("<th>{0}</th>", heading));
            }

            htmlHeadings.Append("</tr></thead>");

            return htmlHeadings.ToString();
        }

        public string RenderCell(TableCell cell)
        {
            string css = "";
            if (!string.IsNullOrEmpty(cell.CssClass))
            {
                css = " class=\"" + cell.CssClass + "\"";
            }
            return string.Format("<td{0}>{1}</td>", css, cell.Contents);
        }

        public string RenderCell(string contents, string cssClass = "")
        {
            return RenderCell(new TableCell() {Contents = contents, CssClass = cssClass});
        }

        public string RenderRow(IEnumerable<string> cells)
        {
            var tableCells = cells.Select(c => new TableCell() {Contents = c}).ToList();
            return RenderRow(tableCells);
        }

        public string RenderRow(IEnumerable<TableCell> cells)
        {
            var htmlRow = new StringBuilder("<tr>");

            foreach (var cell in cells)
            {
                htmlRow.Append(RenderCell(cell));
            }

            htmlRow.Append("</tr>");

            return htmlRow.ToString();
        }
    }
}